package com.wlh.animation.userviews;

import com.wlh.animation.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

/**
 * Created by weilh on 2019-07-05
 */
public class SetPolyToPolyAct extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setpolytopoly);

        ViewGroup container = findViewById(R.id.container);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.google);
        final TestView testView = new TestView(this, bitmap);
        container.addView(testView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        RadioGroup radioGroup = findViewById(R.id.group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                 testView.setPointCount(checkedId - R.id.point0);
            }
        });
    }



    private static class TestView extends View {
        private int triggerRadius = 180;    // 触发半径为180px

        private Paint mPaint;
        private Matrix mMatrix;

        private Bitmap mBitmap;
        private float[] mSrc;
        private float[] mDst;
        private int mPointCount = 0;
        public TestView(Context context, @NonNull Bitmap bitmap) {
            super(context);
            this.mBitmap = bitmap;
            init();
        }

        private void init() {
           // 关闭硬件加速
           setLayerType(LAYER_TYPE_SOFTWARE, null);
           initMatrix();
           initPaint();
        }

        private void initMatrix() {
            mMatrix = new Matrix();
            // 4个角 左上 右上 右下 左下
            mSrc = new float[]{0, 0, mBitmap.getWidth(), 0 , mBitmap.getWidth() , mBitmap.getHeight(),
                    0, mBitmap.getHeight()};
            mDst = mSrc.clone();
            mMatrix.setPolyToPoly(mSrc, 0 , mDst, 0, mPointCount);
            mMatrix.postScale(0.5f, 0.5f);
        }

        public void setPointCount(int pointCount) {
            this.mPointCount = pointCount;
            mDst = mSrc.clone();
            resetPolyMatrix();
        }

        private void initPaint() {
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.RED);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {

            switch (event.getAction()){
                case MotionEvent.ACTION_MOVE:
                    float tempX = event.getX();
                    float tempY = event.getY();

                    // 根据触控位置改变dst
                    for (int i = 0; i < mPointCount ; i++ ) {
                        // 做过一次缩放，用缩放后的点标记
                        if (Math.abs(tempX - dst[2 * i]) <= triggerRadius && Math.abs(tempY - dst[ 2 * i+1]) <= triggerRadius){
                            mDst[2*i]   = (tempX-100) * 2;
                            mDst[2*i+1] = (tempY-100) * 2;
                            resetPolyMatrix();
                            break;  // 防止两个点的位置重合
                        }
                    }
                    break;
            }

            return true;
        }

        private void resetPolyMatrix() {
            mMatrix.reset();
            // 核心要点
            mMatrix.setPolyToPoly(mSrc, 0, mDst, 0, mPointCount);
            mMatrix.postScale(0.5f, 0.5f);
            invalidate();
        }

        float[] dst = new float[8];
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.translate(100, 100);
            canvas.drawBitmap(mBitmap, mMatrix, null);
           // 花移动点的坐标
            mMatrix.mapPoints(dst, mSrc);
            for (int i = 0; i < mPointCount; i++) {
                canvas.drawCircle(dst[i * 2], dst[2 * i + 1], 10, mPaint);
            }
        }
    }

}
